package com.refusea.train.mapper

import com.refusea.train.domain.entity.Course
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Options
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

@Mapper
interface CourseMapper {

    @Insert(
        """
        INSERT INTO course 
        (school_id, name, teacher_id, lesson_count, price, salary, visible, show_order) 
        VALUES 
        (#{schoolId}, #{name}, #{teacherId}, #{lessonCount}, #{price}, #{salary}, #{visible}, #{showOrder})
        """
    )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insert(course: Course): Int

    @Delete("DELETE FROM course WHERE id = #{id} AND school_id = #{schoolId}")
    fun delete(@Param("id") id: Int, @Param("schoolId") schoolId: Int): Int

    @Update(
        """
        UPDATE course SET  
        name = #{name}, 
        teacher_id = #{teacherId}, 
        lesson_count = #{lessonCount}, 
        price = #{price}, 
        salary = #{salary},
        visible = #{visible},  
        show_order = #{showOrder}
        WHERE id = #{id}
        """
    )
    fun update(course: Course): Int

    @Select("SELECT * FROM course WHERE id = #{id}")
    fun select(id: Int): Course?

    @Select("SELECT * FROM course WHERE school_id = #{schoolId} order by show_order")
    fun list(schoolId: Int): List<Course>

    @Update("UPDATE course SET visible = #{visible} WHERE id = #{id} AND school_id = #{schoolId}")
    fun updateVisible(id: Int, schoolId: Int, visible: Boolean): Int

    @Select("SELECT * FROM course")
    fun listAll(): List<Course>
}
